package adv2;

public class Book implements Comparable {
	// 以title作为区分标志
	// 重写Object的equals方法
	private String title, type;
	private int pageNum;

	public Book(String t, int p) {
		title = t;
		pageNum = p;
		type = "计算机类";
	}

	public Book(String title, String type, int pageNum) {
		super();
		this.title = title;
		this.type = type;
		this.pageNum = pageNum;
	}

	@Override
	public String toString() {
		return "Book [title=" + title + ", type=" + type + ", pageNum="
				+ pageNum + "]";
	}

	// @Override
	// public boolean equals(Object obj) {
	// if (!(obj instanceof Book))
	// return false;
	// Book b = (Book) obj;
	// return this.title.equals(b.title);
	// }
	//
	// @Override
	// public int hashCode() {
	// // TODO 自动生成的方法存根
	// return super.hashCode();
	// }

	public String getTitle() {
		return title;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	// 假定页数作为比较的标准
	// >0:this大于o，<0:this小于o
	@Override
	public int compareTo(Object o) {
		if (!(o instanceof Book))
			return Integer.MIN_VALUE;

		return this.title.compareTo(((Book) o).title);

		// return this.pageNum - ((Book) o).pageNum;
	}
}
